﻿/****************************** Module Header ******************************\
 * Module Name:  mxIDataReader.cs
 * Module Version:  1.0.0 (2014-3-14)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension collection instance's method.  It supplies 
 * useful extension methods, such as ToString(), FetchString() and so on.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace UCL.Data
{
    /// <summary>
    /// IDataReader扩展方法
    /// </summary>
    public static class mxIDataReader
    {
        /// <summary>
        /// 将DataReader截取指定单元格合并成字符串
        /// <para>tpl占位符说明：{0}=Name,{1}=Value,{2}=Splitor</para>
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="tpl"></param>
        /// <param name="splitor"></param>
        /// <param name="funcField">对字段值的进一步处理函数</param>
        /// <returns></returns>
        /// <remarks>2014-4-14 新增funcField参数</remarks>
        public static string MergeText(this IDataReader dr, int start = 0, int end = 0, string tpl = "{0}={1}{2}", string splitor = "|", Func<int, object, object> funcField = null)
        {
            if (end == 0 || end >= dr.FieldCount) end = dr.FieldCount;
            if (start > end) return string.Empty;
            StringBuilder sb = new StringBuilder();
            while (start < end)
            {
                sb.AppendFormat(tpl, dr.GetName(start), funcField == null ? dr[start] : funcField(start, dr[start]), splitor);
                start++;
            }
            if (sb.Length == 0) return string.Empty;
            return sb.Remove(sb.Length - splitor.Length, splitor.Length).ToString();
        }

        /// <summary>
        /// 快速获取一个字段的值
        /// </summary>
        /// <param name="iDataReader"></param>
        /// <param name="fieldName"></param>
        /// <param name="varName"></param>
        /// <returns></returns>
        public static bool FetchString(this IDataReader iDataReader, string fieldName, ref string varName)
        {
            bool blnOut = false;
            while (iDataReader.Read())
            {
                blnOut = true;
                varName = iDataReader[fieldName].ToString();
            }
            return blnOut;
        }

        /// <summary>
        /// 快速获取一组字段的值
        /// </summary>
        /// <param name="iDataReader"></param>
        /// <param name="fieldNames"></param>
        /// <param name="varNames"></param>
        /// <returns></returns>
        public static bool FetchStrings(this IDataReader iDataReader, string[] fieldNames, ref string[] varNames)
        {
            bool blnOut = false;
            while (iDataReader.Read())
            {
                blnOut = true;
                int i = 0;
                foreach (string fieldName in fieldNames)
                {
                    varNames[i] = iDataReader[fieldName].ToString();
                    i++;
                }
            }
            return blnOut;
        }
    }
}
